<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 16/4/26
 * Time: 下午2:32
 * Describe:  export d_try data to
 */

namespace console\models;


use common\helper\ArrayHelper;
use common\models\OrderGoods;
use Yii;
use common\models\tmall1\OrderInfo as TOrderInfo;
use common\models\tmall1\OrderGoods as TOrderGoods;
use yii\console\Exception;

class DTryToBestry extends Object
{
    public static $D_TRY_BASE_ID = 40000000;
    public        $isEnd         = false;
    public        $page_size     = 500;
    public        $begin_id      = 39999999;
    public        $end_id        = null;
    public        $ids           = '';

    public function writeData()
    {
        $start_id = $this->begin_id;
        $result   = \services\models\OrderInfo::find()
                                              ->filterWhere(['>', 'order_info.order_info_id', $this->begin_id])
                                              ->innerJoinWith('orderExpress')
                                              ->limit($this->page_size)->all();
        if (empty($result)) {
            $this->isEnd = true;
            return 0;
        }
        /** @var TOrderInfo $oldOrder */
        foreach ($result as $order) {
            static::info("d_try to tmall1 id:{$order->order_info_id} order_sn:{$order->order_sn}");

            $this->begin_id = $order->order_info_id;
            if (!empty($this->end_id) && $order->order_info_id >= $this->end_id) {
                $this->isEnd = true;
                return 0;
            }
            try {
                $oldOrder                = new TOrderInfo();
                $oldOrder->uid           = $order->order_info_id;
                $oldOrder->ordersn       = $order->order_sn;
                $oldOrder->member_uid    = $order->bestry_uid;
                $oldOrder->type          = $order->type;
                $oldOrder->addtime       = strtotime($order->create_time);
                $oldOrder->status        = $order->status;
                $oldOrder->pay_result    = $order->pay_status;
                $oldOrder->pay_id        = $order->pay_id;
                $oldOrder->pay_name      = $order->pay_name;
                $oldOrder->defaultbank   = $order->bank;
                $oldOrder->goods_amount  = $order->goods_amount;
                $oldOrder->order_amount  = $order->order_amount;
                $oldOrder->member_money  = $order->member_money;
                $oldOrder->reduce_amount = $order->reduce_amount;
                $oldOrder->coupon_id     = $order->coupon_id;
                $oldOrder->coupon_code   = $order->coupon_code;
                $oldOrder->coupon_amount = $order->coupon_amount;
                $oldOrder->ship_amount   = $order->ship_amount;
                $oldOrder->is_invoice    = $order->is_invoice;
                $oldOrder->remark        = $order->remark;
                $oldOrder->modify_time   = $order->update_time;
                $oldOrder->jfs           = $order->try_coin;
                $oldOrder->origin        = $order->source;


                $oldOrder->consignee     = $order->orderExpress->consignee;
                $oldOrder->address       = $order->orderExpress->address;
                $oldOrder->s1            = $order->orderExpress->province;
                $oldOrder->s2            = $order->orderExpress->city;
                $oldOrder->s3            = $order->orderExpress->county;
                $oldOrder->address       = $order->orderExpress->address;
                $oldOrder->tel           = $order->orderExpress->telephone;
                $oldOrder->cell          = $order->orderExpress->mobile_phone;
                $oldOrder->delivery_id   = $order->orderExpress->delivery_id;
                $oldOrder->delivery_code = $order->orderExpress->delivery_code;
                $oldOrder->delivery_time = $order->orderExpress->delivery_time;
                $oldOrder->modify_time   = 0;
                $oldOrder->admin_memo    = '';
                $oldOrder->save();
                if ($oldOrder->hasErrors()) {
                    static::info("d_try to tmall1 Failed:{$order->order_info_id} order_sn:{$order->order_sn} " . ArrayHelper::arrayToString($oldOrder->getErrors()));
                }

            } catch (Exception $e) {
                static::info("d_try to tmall1 Failed:{$order->order_info_id} order_sn:{$order->order_sn} " . $e->getMessage());
            }
            unset($order, $oldOrder);


        }

        $result = OrderGoods::find()->filterWhere(['>', 'order_info_id', $start_id])
                            ->andFilterWhere(['<', 'order_info_id', $this->begin_id + 1])
                            ->all();
        /** @var TOrderGoods $tog */
        /** @var OrderGoods $od */
        foreach ($result as $od) {
            #echo $od->order_goods_id, "\n";
            try {
                $tog                = new TOrderGoods();
                $tog->order_id      = $od->order_info_id;
                $tog->goods_id      = $od->goods_id;
                $tog->product_id    = $od->product_id;
                $tog->member_uid    = $od->bestry_uid;
                $tog->buy_number    = $od->buy_number;
                $tog->buy_price     = $od->buy_price;
                $tog->addtime       = strtotime($od->create_time);
                $tog->free_order_id = $od->free_gift;
                $tog->comment_id    = $od->commented;
                $tog->act_id        = $od->activity_id;
                $tog->price_type    = $od->price_type;
                $tog->jfs           = $od->try_coin;
                $tog->save();
                if ($od->hasErrors()) {
                    static::info("d_try to tmall1 order_goods Failed:{$od->order_info_id} product_id:{$od->product_id} " . ArrayHelper::arrayToString($oldOrder->getFirstErrors()));

                }
            } catch (Exception $e) {

                static::info("d_try to tmall1 order_goods Failed:{$od->order_info_id} product_id:{$od->product_id} " . $e->getMessage());
            }

            unset($od, $tog);

        }
    }


    public function run()
    {
        static::info('开始导出从d_try.order_info');
        if (empty($this->end_id)) {
            $max_id         = (int)TOrderInfo::find()->where("uid > " . static::$D_TRY_BASE_ID)->max('uid');
            $max_id         = !$max_id ? static::$D_TRY_BASE_ID - 1 : $max_id;
            $this->begin_id = $max_id;
        }


        for ($times = 0; ; $times++) {
            $this->writeData();
            if ($this->isEnd) {
                break;
            }
        }
        $this->syncGoodsName();
        $this->syncSpecDesc();
        static::info('结束d_try.order_info导入.');
    }

    public function syncGoodsName()
    {
        Yii::$app->db->createCommand("UPDATE tmall1.order_goods g, d_try.goods_detail gd
            SET g.goods_name = gd.goods_name
            WHERE g.goods_id = gd.goods_id AND g.goods_name = '' AND order_id > 40000000;")
                     ->execute();
    }

    public function syncSpecDesc()
    {
        Yii::$app->db->createCommand("UPDATE tmall1.order_goods g, d_try.product_detail gd
            SET g.spec_desc = gd.spec_desc
            WHERE g.product_id = gd.product_id AND g.spec_desc = '' and gd.spec_desc<>'' AND order_id > 40000000;")
                     ->execute();
    }

}